//
//  BHCircleSearchAreaViewController.swift
//  BigHealth
//
//  Created by 蔺百好 on 2021/7/14.
//

import UIKit
import AMapSearchKit

protocol BHCircleSearchAreaViewControllerDelegate {
    func didSearchAreaChose(tip: AMapTip?)
}

class BHCircleSearchAreaViewController: BHBaseViewController {
    var delegate: BHCircleSearchAreaViewControllerDelegate?
    
    
    lazy var searchAPI: AMapSearchAPI = {
        let searchapi = AMapSearchAPI.init()
        searchapi!.delegate = self
        return searchapi!
    }()
    
    lazy var navigationBar_view: BHCircleSearchAreaNavigationBar = {
        let view = BHCircleSearchAreaNavigationBar.loadFromXib()
        view.frame = CGRect(x: 0, y: 0, width: UIScreen.width, height: BHCircleSearchAreaNavigationBar.secHeight)
        view.delegate = self
        return view
    }()
    
    lazy var tableView: UITableView = {
        let view = UITableView.buildTableView(style: .plain)
        view.frame = CGRect(x: 0, y: BHCircleSearchAreaNavigationBar.secHeight, width: UIScreen.width, height: UIScreen.height-BHCircleSearchAreaNavigationBar.secHeight)
        view.delegate = self
        view.dataSource = self
        view.backgroundColor = .white
        view.regiseterNib(nibName: "BHCircleChosePlaceAreaCell")

        return view
    }()
    
    fileprivate var searchAddressArray: [AMapTip] = []
    var searchKey: String = ""
    
    weak var weakSuper: BHBaseViewController?
    override func viewDidLoad() {
        super.viewDidLoad()

        self.bh_navigationBar.removeFromSuperview()
        self.view.addSubview(self.navigationBar_view)
        self.view.addSubview(self.tableView)
        
        self.navigationBar_view.searchTextFiled.becomeFirstResponder()
        self.navigationBar_view.cancelBtn.addBtnAction(action: {[weak self] (_) in
            self?.navigationController?.popViewController(animated: true)
        }, for: .touchUpInside)
    }
    

    func startSearch() {
        let request = AMapInputTipsSearchRequest.init()
        request.city = BHLocationManager.shared.cityName
        request.keywords = self.searchKey
        self.searchAPI.aMapInputTipsSearch(request)
    }
}

extension BHCircleSearchAreaViewController: BHCircleSearchAreaNavigationBarDelegate {
    func clearSearchText() {
        self.searchKey = ""
    }
    
    func goSearchWithKeyword(text: String) {
        self.searchKey = text
        self.startSearch()
    }
    
}

extension BHCircleSearchAreaViewController: AMapSearchDelegate {
    func onReGeocodeSearchDone(_ request: AMapReGeocodeSearchRequest!, response: AMapReGeocodeSearchResponse!) {
        
    }
    
    
    func onInputTipsSearchDone(_ request: AMapInputTipsSearchRequest!, response: AMapInputTipsSearchResponse!) {
        if self.searchAddressArray.count != 0 {
            self.searchAddressArray.removeAll()
        }
        self.searchAddressArray.append(contentsOf: response.tips)
        self.tableView.reloadData()
    }
}

extension BHCircleSearchAreaViewController: UITableViewDataSource {
    
    func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
        self.view.endEditing(true)
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
     
        return self.searchAddressArray.count
    }
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 65
    }
}

extension BHCircleSearchAreaViewController: UITableViewDelegate {

    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "BHCircleChosePlaceAreaCell", for: indexPath) as! BHCircleChosePlaceAreaCell
        let tip = self.searchAddressArray[indexPath.row]
        cell.name_label.text = tip.name
        cell.address_label.text = tip.address
        return cell
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let tip = self.searchAddressArray[indexPath.row]
        self.delegate?.didSearchAreaChose(tip: tip)
        
        if self.weakSuper != nil {
            self.navigationController?.popToViewController(self.weakSuper!, animated: true)
        }else {
            self.navigationController?.popViewController(animated: true)
        }
    }
}
